<template>
<div class="formula-condition-box">
    <div class="formula-condition-toolbar">
        <a-radio-group v-model="source.conditionType" name="radioGroup" @change="onCondition">
            <a-radio value="easy">{{i18n('easy', '简易模式')}}</a-radio>
            <a-radio value="script">{{i18n('senior', '高级模式')}}</a-radio>
        </a-radio-group>
    </div>
    <div class="formula-condition-center">
        <div v-if="source.conditionType ==='easy'">
            <RxConditionGroup ref="easyCondition" :rule-data="source.easyCondition"  :defaultRule="defaultRule"  :level="2" :allowEmpty="true">
                <template slot="conditionDiv" slot-scope="{rule,setValid}">
                    <div class="rx-easy-content-box">
                        <!--       模式                 -->
                        <a-tooltip placement="top">
                            <span  slot="title">{{i18n('mode','模式')}}</span>
                            <!--       意见                 -->
                            <a-select
                                v-model="rule.mode"
                                :options="modeOptions" allowClear
                                @change="selectChange($event,'mode',rule,setValid)"
                            >
                            </a-select>
                        </a-tooltip>

                        <template v-if="['form','flow'].includes(rule.mode)">

                            <a-tooltip placement="top">
                                <span  slot="title">{{i18n('operate','操作')}}</span>
                                <!--       意见                 -->
                                <a-select
                                    v-model="rule.op"
                                    :options="OpOptions[rule.mode]" allowClear>
                                </a-select>
                            </a-tooltip>
                            <template v-if="rule.op === 'approve'">
                                <a-tooltip placement="top">
                                    <span  slot="title">{{i18n('opinion','意见')}}</span>
                                    <!--       意见                 -->
                                    <a-select
                                        v-model="rule.opinion"
                                        :options="opinionOptions" allowClear>
                                    </a-select>
                                </a-tooltip>
                            </template>
                        </template>
                        <template v-else>
                            <a-tooltip placement="top">
                                <span  slot="title">{{i18n('table','表')}}</span>
                                <a-select v-model="rule.table" allowClear>
                                    <a-select-option
                                        :value="item.value"
                                        v-for="item of OpOptions[rule.mode]"
                                        @click="tableChange(item,rule)"
                                    >
                                        {{item.label}}
                                    </a-select-option>
                                </a-select>
                            </a-tooltip>

                            <a-tooltip placement="top">
                                <span  slot="title">{{i18n('field','字段')}}</span>
                                <a-select v-model="rule.field" allowClear>
                                    <a-select-option
                                        :value="item.name"
                                        v-for="item of tableOptions[rule.table]"
                                        @click="fieldChange(item,rule)"
                                    >
                                        {{item.title}}
                                    </a-select-option>
                                </a-select>
                            </a-tooltip>

                            <!--          比较符                  -->

                            <a-tooltip placement="top">
                                <span  slot="title">{{i18n('operator','操作符')}}</span>
                                <a-select
                                    v-model="rule.op" allowClear
                                    :options="fieldOpOptions.filter(item=>item.type.includes(rule.dataType))"
                                >
                                </a-select>
                            </a-tooltip>
                            <template v-if="!['ISNULL','NOTNULL'].includes(rule.op)">
                            <!--          值类型                  -->
                                <a-tooltip placement="top">
                                    <span  slot="title">{{i18n('valType','值类型')}}</span>
                                    <a-select v-model="rule.type" allowClear v-if="rule.dataType==='varchar'">
                                        <a-select-option :value="item.value" v-for="item of typeOptions">
                                            {{item.label}}
                                        </a-select-option>
                                    </a-select>
                                    <span class="x-value-fixed" v-else>固定值</span>
                                </a-tooltip>

                                <a-tooltip placement="top">
                                    <span  slot="title">{{i18n('val','值')}}</span>
                                    <template v-if="rule.dataType=='varchar'">
                                        <template v-if="rule.type === 'context'">
                                            <a-select
                                                v-model="rule.value"
                                                :options="contextOptions" allowClear>
                                            </a-select>
                                        </template>
                                        <template v-else>
                                            <a-input-group compact style="display: flex;">
                                                <a-input  v-model="rule.value" style="flex: 1"  allowClear/>
                                                <a-button><a-icon type="form" /></a-button>
                                            </a-input-group>
                                        </template>
                                    </template>
                                    <template v-else-if="rule.dataType=='date'">

                                        <a-date-picker v-model="rule.value" format="YYYY-MM-DD"
                                                       value-format="YYYY-MM-DD" allowClear/>
                                    </template>
                                    <template v-else>
                                        <a-input-number v-model="rule.value" style="width: 100%" allowClear/>
                                    </template>

                                </a-tooltip>
                            </template>
                        </template>
                    </div>
                </template>
            </RxConditionGroup>
        </div>
        <div v-else >
            <script-component  :value="source.condition" :source="source"></script-component>
        </div>
    </div>
</div>
</template>

<script>
/**
 * @Author : yangsanjin
 * @Date: 2024-09-14 9:27
 */
import { RxMonacoEditor,Dialog,Util,RxConditionGroup } from "@lowcode/jpaas-common-lib"
import ScriptComponent from "@/views/modules/form/core/TableForrmula/ScriptComponent.vue";
import FormBoDefApi from "@/api/form/bo/formBoDef";

export default {
    name: "FormTableFormulaCondition",
    components:{
        RxMonacoEditor,RxConditionGroup,
        ScriptComponent
    },
    props:{
        /**
         * //easy,script
         * conditionType:'',
         * //执行条件
         * condition:{},
         * //简易执行条件
         * easyCondition:{},
         */
        source:{}
    },
    data(){
        return {
            modeOptions:[
                {label:this.i18n('oForm','表单'),value:'form'},
                {label:this.i18n('flow','流程'),value:'flow'},
                {label:this.i18n('field','字段'),value:'field'},
            ],

            OpOptions:{
                form:[
                    {label:this.i18n('saveFlow','保存'),value:'save'},
                    {label:this.i18n('startFlow','启动流程'),value:'start'},
                ],
                flow:[
                    {label:this.i18n('draft','保存草稿'),value:'draft'},
                    {label:this.i18n('startFlow','启动流程'),value:'start'},
                    {label:this.i18n('saveTask','保存任务'),value:'save'},
                    {label:this.i18n('approve','任务审批'),value:'approve'},//选择任务审批后出现意见选择
                ],
                field:[]
            },
            typeOptions:[
                {label:this.i18n('context','上下文'),value:'context'},
                {label:this.i18n('fixed','固定值'),value:'fixed'},
            ],
            contextOptions:[
                {label:this.i18nFormCellRender('form.currentUserID','当前用户ID'),value:"curUserId"},
                {label:this.i18nFormCellRender('form.groupID','当前用户组ID'),value:"curGroupId"}
            ],
            opinionOptions:[
                {label:this.i18n('AGREE','通过'),value:'AGREE'},
                {label:this.i18n('ABSTAIN','弃权'),value:'ABSTAIN'},
                {label:this.i18n('REFUSE','反对'),value:'REFUSE'},
                {label:this.i18n('BACK','驳回'),value:'BACK'},
                {label:this.i18n('BACKSPEC','驳回节点'),value:'BACK_SPEC'},
                {label:this.i18n('BACK_TO_STARTOR','驳回发起人'),value:'BACK_TO_STARTOR'},
            ],
            tableOptions:{},
            fieldOpOptions:[
                {value: 'EQ', label: this.i18nForm('strOptions.eq', '等于'),type:['varchar','number','date','int']},
                {value: 'NEQ',label: this.i18nForm('strOptions.noeq', '不等于'),type:['varchar','number','int']},
                {value: 'CONTAIN', label: this.i18nForm('strOptions.include', '包含'),type:['varchar']},
                {value: 'NOTCONTAIN',label: this.i18nForm('strOptions.notInclude', '不包含'),type:['varchar']},
                {value: 'NIN',label: this.i18nForm('strOptions.notIn', '不包含'),type:['varchar']},
                {value: 'IN',label: this.i18nForm('strOptions.in', '包含于'),type:['varchar']},

                {value: 'LT', label: this.i18nForm('strOptions.under', '小于'),type:['number','date','int']},
                {value: 'LE', label: this.i18nForm('strOptions.leq', '小于等于'),type:['number','date','int']},
                {value: 'GT', label: this.i18nForm('strOptions.overtake', '大于'),type:['number','date','int']},
                {value: 'GE', label: this.i18nForm('strOptions.geq', '大于等于'),type:['number','date','int']},


                {value: 'ISNULL', label: this.i18nForm('strOptions.isEmpty', '为空'),type:['varchar','number','date','int']},
                {value: 'NOTNULL', label: this.i18nForm('strOptions.notEmpty', '不为空'),type:['varchar','number','date','int']},
            ],
            defaultRule:{
                mode:'',//模式
                op:'',
                opinion:'',//意见
                table:'',
                tableType:'',
                field:'',
                dataType:'',
                type:'fixed',
                value:'',
                single: true

            },

        }
    },
    created(){
        this.initFields();
        if(!this.source.conditionType && this.source.condition){
            this.source.conditionType = 'script';
        }
    },
    methods:{
        getEasyCondition(){
            return this.$refs['easyCondition']?.getData()
        },
        i18nFormCellRender(name,text,key){
            return this.$ti18(name,text,"formCellRender",key);
        },
        i18nForm(name, text, key) {
            return this.$ti18(name, text, "formBoDef", key);
        },
        i18n(name, text, key) {
            return this.$ti18(name, text, "formTableFormula.formula", key);
        },
        selectChange(v,type,rule,setValid){
            if(!rule[type]){
                setValid(rule,false,this.i18n('required','必填'))
            }else {
                setValid(rule,true)
            }
            if(type === 'mode'){
                rule.op = ''
            }
        },
        onCondition(e){
            this.source.conditionType = e.target.value;
            this.source.condition='';
            this.source.easyCondition={};
        },
        fieldChange(item,rule){
            rule.dataType = item.dataType ;
            //是否单选
            rule.single = item.isSingle==1 ;

            if(item.dataType=="date"){
                rule.value = Util.formatDate( new Date(),'yyyy-MM-dd');
            }
        },
        tableChange(item,rule){
            rule.tableType = item.tableType ;
        },
        initFields(){
            let boDefId=this.source.boDefId;
            let boName=this.source.boName;
            let self_=this;

            FormBoDefApi.getBoDefConstruct(boDefId,false,'',true).then((res) => {
                let mainFields=res.data.filter(item=>{return item.type==="main"});
                let subTables=res.data.filter(item=>{return item.type!="main"});
                self_.OpOptions["field"].push({
                    label:self_.i18n('main','主表'),
                    value:'main',
                    tableType:'main'
                })
                self_.tableOptions['main'] = [...mainFields]
                for(let i=0;i<subTables.length;i++){
                    let table=subTables[i];
                    if(table.type=="onetoone" || table.name==boName){
                        self_.OpOptions["field"].push({
                            label:table.title,
                            value:table.name,
                            tableType:table.type
                        })
                        self_.tableOptions[table.name] = [...table.children]
                    }
                }
            })
        },
    }

}
</script>

<style scoped>
.formula-condition-box{

    display: grid;
    grid-template-rows: auto 1fr;
}
.formula-condition-toolbar{
    padding: 10px 0;
}
.formula-condition-center{
    overflow: auto;
}
.rx-easy-content-box{
    display: grid;
    grid-template-columns: repeat(auto-fit,minmax(0,1fr));
    gap:0 10px;
}
.x-value-fixed{
    border: 1px solid #d9d9d9;
    border-radius: 4px;
    padding: 0 10px;
    line-height: 30px;
}
</style>